package com.pikaqiu.warehouse.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pikaqiu.warehouse.domain.entity.UserToken;
import com.pikaqiu.warehouse.exception.MiniAuthException;
import com.pikaqiu.warehouse.service.UserTokenService;
import com.pikaqiu.warehouse.mapper.UserTokenMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【user_token】的数据库操作Service实现
* @createDate 2024-08-14 14:44:31
*/
@Service
public class UserTokenServiceImpl extends ServiceImpl<UserTokenMapper, UserToken>
    implements UserTokenService{

    @Override
    public UserToken getByToken(String token) {
        List<UserToken> userTokenList = list(new QueryWrapper<UserToken>().eq("token", token)
            .orderByDesc("expire_date"));
        if (CollectionUtil.isEmpty(userTokenList)) {
            throw new MiniAuthException("登陆已失效，请重新登陆");
        }
        userTokenList.stream().filter(userToken -> userToken.getExpireDate().getTime() > System.currentTimeMillis())
            .findFirst().orElseThrow(() -> new MiniAuthException("登陆已失效，请重新登陆"));
        return userTokenList.get(0);
    }
}




